﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel.DomainServices.Server;
using NetBay.Core.BusinessEntities;
using NetBay.Core.BusinessEntities.Enum;
using Rio.Manager.Business.Repository;

namespace Rio.Applications.Web.Services
{
    /// <summary>
    /// Class to handle Business entities in a RIA services
    /// </summary>
    public partial class BusinessEntitiesDomainService : DomainService
    {
        /// <summary>
        /// Gets all general parameters.
        /// </summary>
        /// <returns></returns>
        [Invoke]
        public IEnumerable<GeneralParameter> GetAllGeneralParameters()
        {
            var parameters = new List<GeneralParameter>();
            try
            {
                parameters = GeneralParameterRepository.GetAllGeneralParameters();
            }
            catch (Exception ex)
            {
                GeneralParameter parameter = new GeneralParameter(ex.Message);
                NetBay.Core.Log.LogManager.Instance().LogError(parameter.FullErrorMessage);
                parameters.Add(parameter);
            }
            return parameters;
        }

        /// <summary>
        /// Gets the general parameter.
        /// </summary>
        /// <returns></returns>
        [Invoke]
        public GeneralParameter GetGeneralParameter(GeneralParameterName generalParameterName)
        {
            var parameter = new GeneralParameter();
            try
            {
                parameter = GeneralParameterRepository.GetGeneralParameter(generalParameterName);
            }
            catch (Exception ex)
            {
                parameter = new GeneralParameter(ex.Message);
                NetBay.Core.Log.LogManager.Instance().LogError(parameter.FullErrorMessage);
            }
            return parameter;
        }

        /// <summary>
        /// Updates the general parameter.
        /// </summary>
        /// <param name="updatedGeneralParameter">The updated general parameter.</param>
        /// <returns></returns>
        [Invoke(HasSideEffects = true)]
        public ReturnBoolean UpdateGeneralParameter(GeneralParameter updatedGeneralParameter)
        {
            ReturnBoolean l_oReturnObject = new ReturnBoolean();
            try
            {
                l_oReturnObject.Value = GeneralParameterRepository.UpdateGeneralParameter(updatedGeneralParameter);
            }
            catch (Exception ex)
            {
                l_oReturnObject = new ReturnBoolean(ex.Message);
                NetBay.Core.Log.LogManager.Instance().LogError(l_oReturnObject.FullErrorMessage);
            }
            return l_oReturnObject;
        }

        /// <summary>
        /// Adds the general parameter.
        /// </summary>
        /// <param name="updatedGeneralParameter">The added general parameter.</param>
        /// <returns></returns>
        [Invoke(HasSideEffects = true)]
        public ReturnBoolean AddGeneralParameter(GeneralParameter addGeneralParameter)
        {
            ReturnBoolean l_oReturnObject = new ReturnBoolean();
            try
            {
                l_oReturnObject.Value = GeneralParameterRepository.AddGeneralParameter(addGeneralParameter);
            }
            catch (Exception ex)
            {
                l_oReturnObject = new ReturnBoolean(ex.Message);
                NetBay.Core.Log.LogManager.Instance().LogError(l_oReturnObject.FullErrorMessage);
            }
            return l_oReturnObject;
        }
    }
}